package MyTest;

//import static org.junit.Assert.assertEquals;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By 

;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit; 

//必须要 因为执行之前要去找参数
@RunWith(Parameterized.class)


public class JUnitTestDemo {
	
	private WebDriver driver;
	  private String baseUrl;
	  private boolean acceptNextAlert = true;
	  private StringBuffer verificationErrors = new StringBuffer();

	  @Before
	  public void setUp() throws Exception {
		  System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		  System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
	    driver = new FirefoxDriver();
	    baseUrl = "http://cn.onlinebmicalculator.com/ ";
	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  }
	
	String myh;
	String myw;
	String mybmi;

	@Parameters    
	public static Collection data() {
	        return Arrays.asList(new Object[][]{
	                {"150","60","24"},
	                {"150","70","24"},
	                {"150","80","24"},
	        });
	}
	
	//构造函数，对变量进行初始化
	public JUnitTestDemo(String h, String w,String bmi) {
	        this.myh = h;
	        this.myw = w;
	        this.mybmi = bmi;
	}

	@Test
	public void testLgy() throws Exception {
	    driver.get(baseUrl + "/");
	    assertEquals("免费在线 BMI 计算器 - 免费计算你的身体质量指数 (BMI)", driver.getTitle());
	    driver.findElement(By.name 

("f_height")).clear();
	    driver.findElement(By.name 

("f_height")).sendKeys(this.myh);
	    driver.findElement(By.name 

("f_weight")).clear();
	    driver.findElement(By.name 

("f_weight")).sendKeys(this.myw);
	    driver.findElement(By.name 

("f_submit")).click();
	    assertEquals("免费在线 BMI 计算器 - 免费计算你的身体质量指数 (BMI)", driver.getTitle());
	    WebElement element = driver.findElement(By.className("bmi_info")).findElement(By.tagName("span"));
	    String bmi = element.getText();
	    System.out.print("BMI:"+bmi);
	    Assert.assertEquals(this.mybmi, bmi);
	  }

	  @After
	  public void tearDown() throws Exception {
	    driver.quit();
	    String verificationErrorString = verificationErrors.toString();
	    if (!"".equals(verificationErrorString)) {
	      fail(verificationErrorString);
	    }
	  }

	  private boolean isElementPresent(By by) {
	    try {
	      driver.findElement(by);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	  }

	  private boolean isAlertPresent() {
	    try {
	      driver.switchTo().alert();
	      return true;
	    } catch (NoAlertPresentException e) {
	      return false;
	    }
	  }

	  private String closeAlertAndGetItsText() {
	    try {
	      Alert alert = driver.switchTo().alert();
	      String alertText = alert.getText();
	      if (acceptNextAlert) {
	        alert.accept();
	      } else {
	        alert.dismiss();
	      }
	      return alertText;
	    } finally {
	      acceptNextAlert = true;
	    }
	  }

}